// pages/completeInfo/completeInfo.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        date: "请选择日期",
        member_nick_name:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var data = res.data;
                var member_id = data.id;
                var member_phone_number = data.phone_number;
                var open_id = data.open_id;
                var nick_name = data.nickname;
                var birthday = app.to_time(data.birthday);
                that.setData({
                    member_id: member_id,
                    member_phone_number: member_phone_number,
                    open_id: open_id,
                    member_nick_name: nick_name,
                    date: birthday,
                })
            },
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    bindDateChange: function (e) {
        this.setData({
            date: e.detail.value
        })
    },
    change_number: function () {
        wx.navigateTo({
            url: '../../pages/changePhoneNumber/changePhoneNumber',
        })
    },//获取会员昵称
    member_nick_name: function (e) {
        var that = this;
        var member_nick_name = e.detail.value;
        that.setData({
            member_nick_name: member_nick_name
        })
    },//获取会员手机号码
    member_phone_number: function (e) {
        var that = this;
        var member_phone_number = e.detail.value;
        that.setData({
            member_phone_number: member_phone_number
        })
    },
    submit: function () {
        var that = this,
            member_nick_name = that.data.member_nick_name,
            member_id = that.data.member_id,
            open_id = that.data.open_id,
            date = that.data.date,
            params = {};
        params['id'] = member_id;
        params['nickname'] = member_nick_name
        params['birthday'] = Date.parse(new Date(date)) / 1000;
        wx.request({
            url: app.config() + "member/xiaoUpdateNickBirth",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                
                if(res.data.status==0){
                    that.get_member_info(open_id);
                    wx.showToast({
                        title: '保存成功',
                        icon: "success"
                    })
                    wx.switchTab({
                        url: '../../pages/myPage/myPage',
                    })
                }else{
                    wx.showToast({
                        title: res.data.message,
                        image:"../../images/warning.png"
                    })
                }
            }
        })
    },
    get_member_info: function (open_id) {
        var that = this,
            params = {};
        params['open_id'] = open_id;
        wx.request({
            url: app.config() + "member/xiaoLoginByWx",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var data = res.data;
                if (data.status == 0) {
                    // 登陆成功
                    data.data['open_id'] = open_id;
                    wx.setStorage({
                        key: 'memberInfo',
                        data: data.data
                    })
                    
                } else {
                    wx.showToast({
                        title: res.data.message,
                        image:"../../images/warning.png"
                    })
                }
            }
        })
    }
})